﻿Imports MapWinGIS
Imports System.ComponentModel
<Serializable()> Public Class ESPointLayer
    Inherits EditableShapeLayer


    Public Sub New(ByVal ln As String, ByVal tl As BaseTool)
        '目前为止，只支持pointtool

        MyBase.New(ln, tl)


        pointtype = FtkPointType.ptCircle
        pointsize = 4
        pointColor = Drawing.Color.Black


    End Sub

    '这些都是为了讨厌的VB事件逻辑啊
    Protected Sub New( _
      ByVal info As System.Runtime.Serialization.SerializationInfo, _
      ByVal context As System.Runtime.Serialization.StreamingContext)

        MyBase.New(info, context)

    End Sub


    '私有的一些属性
    Dim pointtype As FtkPointType
    <CategoryAttribute("外观设置"), _
   DisplayName("点类型")> _
   Public Overridable Property PointtypeProperty() As FtkPointType
        Get
            Return pointtype
        End Get
        Set(ByVal value As FtkPointType)
            pointtype = value

            Me.OnPtTypeChanged(pointtype)

        End Set
    End Property

    Dim pointColor As Drawing.Color
    <CategoryAttribute("外观设置"), _
   DisplayName("点颜色")> _
   Public Overridable Property PointColorProperty() As Drawing.Color
        Get
            Return pointColor
        End Get
        Set(ByVal value As Drawing.Color)
            pointColor = value

            Me.OnPtClChanged(pointColor)
        End Set
    End Property
    Dim pointsize As Double
    <CategoryAttribute("外观设置"), _
   DisplayName("点大小")> _
   Public Overridable Property PointsizeProperty() As Double
        Get
            Return pointsize
        End Get
        Set(ByVal value As Double)
            pointsize = value

            Me.OnPtSzChanged(pointsize)

        End Set
    End Property
    Dim ptUdpImg As String
    <CategoryAttribute("外观设置"), _
   DisplayName("自定义点图片"), _
   Editor(GetType(System.Windows.Forms.Design.FileNameEditor), _
 GetType(System.Drawing.Design.UITypeEditor))> _
    Public Overridable Property PtUdpImgProperty() As String
        Get
            Return ptUdpImg
        End Get
        Set(ByVal value As String)
            ptUdpImg = value

            Me.OnPtUdpImgChanged(ptUdpImg)

        End Set
    End Property


    '对应的处理函数
    Private Sub OnPtSzChanged(ByVal n As Double)

        mapwin.set_ShapeLayerPointSize(m_iLayer, n)
    End Sub

    Private Sub OnPtClChanged(ByVal cl As Drawing.Color)

        Dim ucl As UInt32 = RGB(cl.R, cl.G, cl.B)
        mapwin.set_ShapeLayerPointColor(m_iLayer, ucl)

    End Sub

    Private Sub OnPtTypeChanged(ByVal pt As MapWinGIS.tkPointType)

        Dim temp As MapWinGIS.tkPointType = Me.getPointType(pt)

        mapwin.set_ShapeLayerPointType(m_iLayer, temp)

    End Sub


    Private Sub OnPtUdpImgChanged(ByVal img As String)


        Dim ig As New MapWinGIS.Image

        ig.Open(img)


        mapwin.set_UDPointType(m_iLayer, ig)



    End Sub

    Public Overrides Sub AddEntityRange(ByVal lst As List(Of FEntity))

        For Each en As FEntity In lst

            dicKeyPts.Add(en.sn, en)
        Next





        Dim ptlist As MapWinGIS.Point()
        Dim sn As String
        Dim desc As String

        Dim sfPoint As MapWinGIS.Shapefile = mapwin.get_GetObject(m_iLayer)



        sfPoint.StartEditingShapes(True)



        For Each en As FEntity In lst



            sn = en.sn
            ptlist = en.ptlist
            desc = en.desc


            If ptlist.Length < 1 Then

            Else


                Dim shp As New Shape
                shp.ShapeType = ShpfileType.SHP_POINT
                Dim ptIndex As Integer = 0

                shp.InsertPoint(ptlist(0), ptIndex)




                Dim shpIndex As Integer = sfPoint.NumShapes

                sfPoint.EditInsertShape(shp, shpIndex)

                sfPoint.EditCellValue(0, shpIndex, sn)
                sfPoint.EditCellValue(1, shpIndex, desc)





            End If





        Next

        sfPoint.StopEditingShapes()


        '由于新增加了对象，所以需要根据现有状态，对对象操作

        Me.DrawText()

        mapwin.Redraw()

    End Sub



    Public Overrides Function AddEntity(ByVal sn As String, ByVal ptlist() As MapWinGIS.Point, ByVal desc As String) As Integer
        If ptlist.Length < 1 Then

            Return -1
        End If

        '关键节点，呵呵
        Dim en As New FEntity(sn, ptlist, desc)
        dicKeyPts.Add(sn, en)





        Dim sfPoint As MapWinGIS.Shapefile = mapwin.get_GetObject(m_iLayer)

        sfPoint.StartEditingShapes(True)





        Dim shp As New Shape
        shp.ShapeType = ShpfileType.SHP_POINT
        Dim ptIndex As Integer = 0

        shp.InsertPoint(ptlist(0), ptIndex)




        Dim shpIndex As Integer = sfPoint.NumShapes

        sfPoint.EditInsertShape(shp, shpIndex)

        sfPoint.EditCellValue(0, shpIndex, sn)
        sfPoint.EditCellValue(1, shpIndex, desc)

        sfPoint.StopEditingShapes()

        '由于新增加了对象，所以需要根据现有状态，对对象操作

        Me.DrawText()

        'Dim ucl As UInt32 = RGB(cl.R, cl.G, cl.B)

        'Me.DrawShapeName(name, shpIndex, 0, ucl)



        mapwin.Redraw()

        Return shpIndex

    End Function

    Public Overrides Sub InitNewLayer(ByRef m As AxMapWinGIS.AxMap, ByVal OutPath As String)
        Dim sf As New Shapefile
        Dim strPath(3) As String


        'strPath(0) = "C:\temp\" + lname + ".shp"
        'strPath(1) = "C:\temp\" + lname + ".dbf"
        'strPath(2) = "C:\temp\" + lname + ".shx"

        'If System.IO.File.Exists(strPath(0)) Then
        '    System.IO.File.Delete(strPath(0))
        '    System.IO.File.Delete(strPath(1))
        '    System.IO.File.Delete(strPath(2))

        'End If

        sf.CreateNew("", MapWinGIS.ShpfileType.SHP_POINT)




        Dim f As New Field

        f = New Field
        f.Name = "Name"
        f.Type = FieldType.STRING_FIELD
        f.Width = 50


        Dim fd As New Field

        fd = New Field
        fd.Name = "desc"
        fd.Type = FieldType.STRING_FIELD
        fd.Width = 50


        sf.StartEditingShapes(True)
        sf.EditInsertField(f, 0)

        sf.EditInsertField(fd, 1)

        sf.StopEditingShapes(True, True)


        '加入载入的逻辑

        Dim strSave As String = OutPath + lname + ".shp"

        strPath(0) = OutPath + lname + ".shp"
        strPath(1) = OutPath + lname + ".dbf"
        strPath(2) = OutPath + lname + ".shx"

        For i As Integer = 0 To strPath.Length - 1
            If System.IO.File.Exists(strPath(i)) Then
                Try
                    System.IO.File.SetAttributes(strPath(i), IO.FileAttributes.Normal)
                    System.IO.File.Delete(strPath(i))
                Catch ex As Exception

                End Try

            End If
        Next

        Dim bS As Boolean = sf.SaveAs(strSave)

        If Not bS Then
            System.Windows.Forms.MessageBox.Show(lname + "文件创建失败！，请查看电脑软件使用权限")

        End If

        fname = strSave
        Me.InitLayer(m)
    End Sub

    Public Overrides Sub InitProp()
        Me.OnPtTypeChanged(pointtype)
        Me.OnPtClChanged(pointColor)
        Me.OnPtSzChanged(pointsize)
        Me.OnPtUdpImgChanged(ptUdpImg)

    End Sub

    ''' <summary>
    ''' 只在加载外部shp文件时，在最开始部分调用此函数
    ''' 非常规的调用会引发程序错误
    ''' </summary>
    ''' <remarks></remarks>
    Public Overrides Sub BuildKeyPtsFromFile()
        Dim sf As MapWinGIS.Shapefile = mapwin.get_GetObject(m_iLayer)
        For i As Integer = 0 To sf.NumShapes
            Dim en As New FEntity()
            en.sn = Me.GetShapeFieldString(i, 0)
            en.desc = Me.GetShapeFieldString(i, 1)
            en.ptlist = sf.QuickPoints(i, sf.Shape(i).numPoints)


        Next

    End Sub
End Class
